package com.dfbz.controller;

import com.alibaba.fastjson.JSON;
import com.dfbz.service.HomeService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/home/detail")
public class HomeServlet extends HttpServlet {
    private HomeService homeService = new HomeService();
    @Override
    /**
     * 后端首页新增文章/会议/人员信息ajax反馈
     */
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String,Object> mapCount = homeService.findCount();
        List<Map<String,Object>> mapDetailInfo = homeService.findDetailInfo();
        List<List> infoList = new ArrayList<>();
        List list ;
        for (Map<String, Object> map : mapDetailInfo) {
            list = new ArrayList();
            for (String key : map.keySet()) {
                list.add(map.get(key));
            }
            infoList.add(list);
        }
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("mapCount",mapCount);
        hashMap.put("infoList",infoList);
        response.setContentType("application/json;charset=utf-8");
        String json = JSON.toJSONString(hashMap);
        response.getWriter().write(json);
    }
}
